<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/9 0009
 * Time: 15:46
 */

namespace app\payment\controller;


use app\common\controller\Homebase;
use app\member\model\Member as Member_Model;
use app\Payment\model\PayAccount as PayAccount_Model;
class Returnurl extends Homebase
{
    //初始化
    protected function initialize()
    {
        parent::initialize();
        header('Access-Control-Allow-Origin:*');
        $this->PayAccount_Model=new PayAccount_Model;
        $this->Member_Model=new Member_Model;


    }
    //支付宝
    public function index(){
        $config =Config('alipay.alipay') ;
        require_once EXTEND_PATH.'pay/alipay/AlipayTradeService.php';
       $pay_order= session('pay_order');
        $where=['trade_sn'=>$pay_order];
        $order_res= $this->PayAccount_Model->get_one($where);
        $arr=$_GET;
        $alipaySevice = new \AlipayTradeService($config);
        $result = $alipaySevice->check($arr);
        if(!$result) {//验证成功
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //请在这里加上商户的业务逻辑程序代码

            //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
            //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表

            //商户订单号
            $out_trade_no = htmlspecialchars($_GET['out_trade_no']);

            //支付宝交易号
            $trade_no = htmlspecialchars($_GET['trade_no']);
            $data['paytime']=time();
            $data['status']='succ';
            $where=['trade_sn'=>$pay_order];
            if($this->PayAccount_Model->updateOrderData($where,$data)){
                $mwere=['id'=>$order_res['userid']];
                $mdata['field']='amount';
                $mdata['value']=$order_res['money']*$order_res['quantity'];
                if($this->Member_Model-> updateBalance($mwere,$mdata)){
                    $this->success('支付成功', url('member/index/index',['order_sn'=>$order_res['order_sn']]),['msg_title'=>'提交成功,进入确认页面']);
                  // header('Location:'.url('member/index/index'));
                }else{
                    $this->error('支付失败', url('member/OnlineRecharge/payment_log',['order_sn'=>$order_res['order_sn']]),['msg_title'=>'提交成功,进入确认页面']);

                }
            }else{
                $this->error('支付失败', url('member/OnlineRecharge/payment_log',['order_sn'=>$order_res['order_sn']]),['msg_title'=>'提交成功,进入确认页面']);

            }



        }
        else {
            //验证失败
            echo "验证失败";
        }
     //  print_r($pay_order);
    }

}